from django.shortcuts import render
from rest_framework.generics import ListAPIView

from .models import SKU
from .serializers import SKUSerializer
from rest_framework.filters import OrderingFilter

# Create your views here.

class SKUListView(ListAPIView):
    """商品列表数据查询"""

    filter_backends = [OrderingFilter] #指定过滤后端为排序过滤
    ordering_fields = ['create_time', 'price', 'sales'] #指定排序字段

    serializer_class = SKUSerializer
    # queryset = SKU

    def get_queryset(self):
        """如果当前在试图中没有去定义 get/post 方法 没法定义一个参数来接收正则组提取出来的url路径参数
            可以利用试图对象的args或者kwargs属性来获取
        """
        category_id = self.kwargs.get('category_id')
        return SKU.objects.filter(is_launched=True, category_id=category_id)

